package com.example.tianling.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.params.AiResultDo;
import com.example.tianling.common.utils.MinioUtils;
import com.example.tianling.common.vo.AiResultListVo;
import com.example.tianling.common.vo.AiResultVo;
import com.example.tianling.common.vo.TrainListVo;
import com.example.tianling.dao.AiResultMapper;
import com.example.tianling.entity.AiResultEntity;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.entity.RoomEntity;
import com.example.tianling.entity.TrainResultEntity;
import com.example.tianling.service.AiResultService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AiResultServiceImpl extends ServiceImpl<AiResultMapper, AiResultEntity> implements AiResultService {

    @Autowired
    private MinioUtils minioUtils;

    @Override
    public void saveResult(Integer userId, AiResultDo aiResultDo) throws Exception {
        // 将录音文件上传到文件服务器
//        JSONObject res = minioUtils.uploadAiResult(aiResultDo.getType(), aiResultDo.getFile());

        // 封装AiResult对象
        AiResultEntity aiResult = new AiResultEntity();
        aiResult.setUserId(userId);
        aiResult.setBookId(aiResultDo.getBookId());
        aiResult.setType(aiResultDo.getType());
        aiResult.setRecord(aiResultDo.getRecord());
        aiResult.setText(aiResultDo.getText());
        aiResult.setAiComment(aiResultDo.getAiComment());
        aiResult.setTime(new Date());

        // 保存到数据库
        baseMapper.insert(aiResult);
    }

    @Override
    public List<AiResultListVo> getAiResultList(Integer userId) {
        MPJLambdaWrapper<Object> wrapper = new MPJLambdaWrapper<>()
                .select(AiResultEntity::getId, AiResultEntity::getType, AiResultEntity::getTime)
                .select(BookEntity::getBookName, BookEntity::getDescription)
                .eq(AiResultEntity::getUserId, userId)
                .orderByDesc(AiResultEntity::getTime)
                .leftJoin(BookEntity.class, BookEntity::getBookId, AiResultEntity::getBookId);
        List<AiResultListVo> resultList = baseMapper.selectJoinList(AiResultListVo.class, wrapper);
        return resultList;
    }

    @Override
    public AiResultVo getAiResult(Integer id) {
        MPJLambdaWrapper<Object> wrapper = new MPJLambdaWrapper<>()
                .select(BookEntity::getBookName, BookEntity::getContent, BookEntity::getAnswer, BookEntity::getIdeaOne, BookEntity::getIdeaTwo, BookEntity::getIdeaThree)
                .select(AiResultEntity::getRecord, AiResultEntity::getText, AiResultEntity::getAiComment, AiResultEntity::getTime, AiResultEntity::getNote)
                .eq(AiResultEntity::getId, id)
                .leftJoin(BookEntity.class, BookEntity::getBookId, AiResultEntity::getBookId);
        AiResultVo aiResultVo = baseMapper.selectJoinOne(AiResultVo.class, wrapper);
        return aiResultVo;
    }

    @Override
    public void updateNote(Integer id, String note) {
        this.lambdaUpdate().eq(AiResultEntity::getId, id).set(AiResultEntity::getNote, note).update();
    }


}
